import mapper from "../mapper/BaseMapper.js";
import BeanUtils from "../utils/BeanUtils.js";
import CourseVO from "../domain/vo/CourseVO.js";
import CourseDetailVO from "../domain/vo/CourseDetailVO.js";

export default class CourseServiceImpl {



    getCourseList() {
        return mapper.get("tb_course").map(i => BeanUtils.toBean(new CourseVO(), i))
    }

    getCourseListByTag(tag, page, limit) {
        const res = []
        let offset = (page - 1) * limit

        const list = mapper.get("tb_course")
        for (let i = 0; i < list.length; i ++) {
            const tags = list[i].tags.split(",")
            if (limit == 0) {
                break
            }
            if (tags.indexOf(tag) != -1) {
                if (offset == 0) {
                    limit --
                    res.push(list[i])
                }
            }
        }

        return res
    }

    getHotCourseList(tag, limit) {
        let list = mapper.get("tb_course")
        if (tag) {
            list = list.filter(i => {
                return i.tags.split(",").indexOf(tag) != -1
            })
        }
        
        const res = list.toSorted((a, b) => {
            return b.joinLearning - a.joinLearning
        })

        return res.slice(0, limit)
    }

    getRecommendCourseList(tag, limit) {
        let list = mapper.get("tb_course").filter(i => i.recommend)
        if (tag) {
            list = list.filter(i => i.tags.split(",").indexOf(tag) != -1)
        }
        return list.slice(0, limit)
    }

    getCourseDetail(id) {
        return mapper.get("tb_course").find(i => i.id == id)
    }

}